package com.hospital.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/11/19 13:29
 */
@Configuration
public class ShrioConfig {

    @Resource
    private MyRealm myRealm;

    // 配置SecurityManager
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("webSessionManager") DefaultWebSessionManager sessionManager){
        // 1.创建defaultWebSecurityManager 对象
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setSessionManager(sessionManager);
        // 2.创建加密对象，设置相关属性
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        // 2.1采用md5加密
        matcher.setHashAlgorithmName("md5");
        // 2.2采用迭代加密的次数
        matcher.setHashIterations(3);
        // 3.将加密对象存储刀到myRealm中
        myRealm.setCredentialsMatcher(matcher);
        // 4.将myRealm存入defaultWebSecurityManager 对象
        defaultWebSecurityManager.setRealm(myRealm);
        // 5.返回
        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager")DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //权限设置：
        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        //设置登录页面
        shiroFilterFactoryBean.setLoginUrl("/login");
        //设置退出页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/login");
        //设置没有权限跳转的页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
        //注意优先级从上到下依次降低---
        //登录页面放行
        map.put("/login","anon");
        map.put("/","anon");
        //登录请求放行
        map.put("/userLogin","anon");
        //注册页面放行
        //所有的静态文件都放行
        map.put("/files/**", "anon");
        map.put("/files/doctor/**","anon");
        map.put("/assets/css/**", "anon");
        map.put("/assets/fonts/**", "anon");
        map.put("/assets/images/**", "anon");
        map.put("/assets/js/**", "anon");
        map.put("/assets/bundles/**", "anon");
        map.put("/assets/plugins/**", "anon");
        map.put("/assets/**","anon");
        //进入的所有页面都要登录
        map.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }


    /**
     * 解决shiro的URL中出现sessionID的情况
     * EG: localhost:8080/toLogin;jsessionid=D5C1EE61B97EE2D7098F58A837B82BD4
     * @return
     */
    @Bean
    @Qualifier("webSessionManager")
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
        defaultWebSessionManager.setSessionIdUrlRewritingEnabled(false);
        return defaultWebSessionManager;
    }


}
